import numpy as np
from scipy import signal
from sklearn.decomposition import FastICA
import matplotlib.pyplot as plt
import librosa
from pylab import mpl
# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False
audio_path = r"E:\dogbarkdata\AudioClassification\dataset\dogbarking\audio\fold0\0gkLHfHJSnI_80_90_cut.mp3"
y, sr = librosa.load(audio_path)
X = y.reshape(-1, 1)
# ICA 处理
ica = FastICA(n_components=3)
S_ = ica.fit_transform(X)  # 估计独立成分
A_ = ica.mixing_  # 估计混合矩阵

# 绘图比较
plt.figure(figsize=(8, 8))

models = [X, S_]
names = ['观测信号', 'ICA 估计信号']
colors = ['red', 'orange']

print(X)
print(S_)
a= np.arange(len(X)) / sr
plt.plot(a,X,color = 'r',label="观测信号")#s-:方形
plt.plot(a,S_[:, 0],color = 'g',label="ICA 估计信号")#o-:圆形
plt.xlabel("时间 (秒)")#横坐标名字
plt.ylabel("振幅")#纵坐标名字

plt.legend(loc = "best")#图例
plt.show()